﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniMana.Utils.Controls;
using UniMana.Utils.Configs;
using UniMana.Utils.Helper;
using UniMana.Utils.Data;

namespace UniMana.Data
{
    public class Training_Student_Subject_ClassInfo
    {
        #region /*Không thay đổi*/
            public bds m_bds = new bds();
            public void Save()
            {
                m_bds.EndEdit();
                m_data.m_DCSave.SubmitChanges();
            }
            public void Delete()
            {
                m_bds.DeleteCurrent();
            }
            #endregion /*Không thay đổi*/
            private DBMain m_data = new DBMain();
            #region [Properties - Thuộc tính]
        
            public Training_Student_Subject_Class Properties
            {
                get { return m_bds.Current as Training_Student_Subject_Class; }
            }
            #endregion
            public Training_Student_Subject_ClassInfo(string studentKey = DefaultConfig.EMPTY_GUID, int subjectClassKey = 0)
            {
                var IQuery = m_data.m_DCSave.Training_Student_Subject_Classes.Where(tb => tb.Training_StudentKey == Guid.Parse(studentKey) &&
                                                                        tb.Training_Subject_ClassKey == subjectClassKey);
                m_bds.DataSource = IQuery;
                var Row = IQuery.FirstOrDefault();
                if (Row == null)
                {
                    Training_Student_Subject_Class _new = new Training_Student_Subject_Class();
                    _new.Training_StudentKey = Guid.Parse(studentKey);
                    _new.Training_Subject_ClassKey = subjectClassKey;
                    Row = _new;
                    m_bds.Add(Row);
                }
            }

            #region Chi --Hòa
            private bool m_bExist = true;

            public bool ExistInDataBase
            {
                get { return this.m_bExist; }
            }
            public Training_Student_Subject_ClassInfo(
                int _iTraining_Subject_ClassKey = -1,
                string _strTraining_StudentKey = UniMana.Utils.Configs.DefaultConfig.EMPTY_GUID,
                int _iTraining_TrainingProgramKey = -1)
            {
                var iQuery = this.m_data.m_DCSave.Training_Student_Subject_Classes.Where(
                    p => p.Training_Subject_ClassKey == _iTraining_Subject_ClassKey &&
                        p.Training_StudentKey == _strTraining_StudentKey.ToGuid() &&
                        p.Training_TrainingProgramKey == _iTraining_TrainingProgramKey);
                this.m_bds.DataSource = iQuery;
                var row = iQuery.FirstOrDefault();
                this.m_bExist = true;
                if (row == null)
                {
                    this.m_bExist = false;
                    row = new Training_Student_Subject_Class();
                    row.Training_StudentKey = _strTraining_StudentKey.ToGuid();
                    row.Training_Subject_ClassKey = _iTraining_Subject_ClassKey;
                    row.Training_TrainingProgramKey = _iTraining_TrainingProgramKey;
                    this.m_bds.Add(row);
                    this.m_bds.MoveLast();
                }
            }
            #endregion

    }
}
